﻿using System;
using System.Collections.Generic;
using System.Text;
using MediaCenterCommander.RemoteInterfaces;
using System.Runtime.Remoting;

namespace MediaCenterCommander.LocalControl
{
    public static class TelevisionLocalController
    {
        public static ITelevisionController GetTelevisionController(string name)
        {
            // TODO: Get television controller from service registry.

            string uri = "tcp://127.0.0.1:9900/ITelevisionController";

            if (uri != null)
            {
                return (ITelevisionController)RemotingServices.Connect(typeof(ITelevisionController), uri);
            }
            else
            {
                return null;
            }
        }

        public static bool IsPoweredOn
        {
            get
            {
                var tv = GetTelevisionController(null);
                if (tv != null)
                {
                    return tv.IsPoweredOn;
                }
                else
                {
                    return false;
                }
            }
        }

        public static void PowerOn()
        {
            var tv = GetTelevisionController(null);
            if (tv != null)
            {
                tv.PowerOn();
            }
        }

        public static void PowerOff()
        {
            var tv = GetTelevisionController(null);
            if (tv != null)
            {
                tv.PowerOff();
            }
        }

        public static int VolumeLevel
        {
            get
            {
                var tv = GetTelevisionController(null);
                if (tv != null)
                {
                    return tv.VolumeLevel;
                }
                else
                {
                    return 0;
                }
            }

            set
            {
                var tv = GetTelevisionController(null);
                if (tv != null)
                {
                    tv.VolumeLevel = value;
                }
            }
        }

        public static void VolumeUp()
        {
            var tv = GetTelevisionController(null);
            if (tv != null)
            {
                tv.VolumeUp();
            }
        }

        public static void VolumeDown()
        {
            var tv = GetTelevisionController(null);
            if (tv != null)
            {
                tv.VolumeDown();
            }
        }

        public static void MuteToggle()
        {
            var tv = GetTelevisionController(null);
            if (tv != null)
            {
                tv.MuteToggle();
            }
        }

        public static void MuteOn()
        {
            var tv = GetTelevisionController(null);
            if (tv != null)
            {
                tv.MuteOn();
            }
        }

        public static void MuteOff()
        {
            var tv = GetTelevisionController(null);
            if (tv != null)
            {
                tv.MuteOff();
            }
        }

        public static bool IsMuted
        {
            get
            {
                var tv = GetTelevisionController(null);
                if (tv != null)
                {
                    return tv.IsMuted;
                }

                return false;
            }
        }
    }
}
